package com.sskj.app.util.cacheUtil;

import com.sskj.app.config.redis.MyRedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.*;

/**
 * @author 张子艺
 * @packge com.util.cache
 * @data 2019-01-10 13:45
 * @project Currency
 */
@Component
public class JedisUtil {
    private final Logger logger = LoggerFactory.getLogger(JedisUtil.class);

    @Autowired
    MyRedisUtil redisManager;

   JedisUtil(){
       System.err.println("正在注入jedis！！！！" );
   }

    /**
     * 设置 list
     *
     * @param <T>
     * @param key
     */
    public <T> void setList(String key, List<T> list,Integer time) {
        try {
            redisManager.set(key.getBytes(), ObjectTranscoder.serialize(list),time);
        } catch (Exception e) {
            e.printStackTrace(System.out);
            logger.error("设置List类型缓存时出错");

        }
    }

    /**
     * 获取list
     *
     * @param <T>
     * @param key
     * @return list
     */
    public <T> List<T> getList(String key) {
        byte[] in = redisManager.get(key.getBytes());
        List<T> list = (List<T>) ObjectTranscoder.deserialize(in);
        return list;
    }


    /**
     * 设置 map
     *
     * @param <T>
     * @param key
     */
    public <T> void setMap(String key, Map<String, T> map,Integer time) {
        try {
            redisManager.set(key.getBytes(), ObjectTranscoder.serialize(map));
        } catch (Exception e) {
            e.printStackTrace(System.out);
            logger.error("设置Map类型缓存时出错");
        }
    }

    /**
     * 获取Map
     *
     * @param <T>
     * @param key
     * @return list
     */
    public <T> Map<String, T> getMap(String key) {

        byte[] in = redisManager.get(key.getBytes());
        Map<String, T> map = (Map<String, T>) ObjectTranscoder.deserialize(in);
        return map;
    }

    /**
     * 设置 过期时间
     *
     * @param key
     * @param value   新值
     */
    public void setString(String key,  String value,Integer time) {
        try {
            value = value==null||value.equals("") ? "" : value;
            redisManager.set(key.getBytes(), value.getBytes(), time);
        } catch (Exception e) {
            logger.error("设置" + key + "的缓存时间时候出错 ");
            e.printStackTrace(System.out);
        }
    }

    /**
     * 获取String值
     *
     * @param key
     * @return value
     */
    public String getString(String key) {
        return new String(redisManager.get(ObjectTranscoder.serialize(key)));
    }

    /**
     * 设置String值
     *
     * @param key
     * @return value
     */
    public String setString(String key, String value) {
        return new String(redisManager.set(key.getBytes(), value.getBytes()));
    }

    /**
     * 删除一个key
     */
    public void del(String key) {
        redisManager.del(key.getBytes());
    }

    /**
     * 模糊删除
     */
    public void delByParam(String keyParam) {
        Set<byte[]> strings = redisManager.keys(keyParam);
        for (byte[] key : strings) {
            redisManager.del(key);
        }

    }



    /**
     * 更新一个key
     */
    public void update(String key, Object value,Integer time) {
        redisManager.del(key.getBytes());
        if (value instanceof List) {
            setList(key, (List<? extends Object>) value,time);
        }
        if (value instanceof Map) {
            setMap(key, (Map<String, ? extends Object>) value,time);
        }
        if (value instanceof String) {
            setString(key, (String) value,time);
        }
    }


    /**
     * byte转对象
     *
     * @param bytes
     * @return
     */
    private Object ByteToObject(byte[] bytes) {
        Object obj = null;
        try {
            // bytearray to object
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ObjectInputStream oi = new ObjectInputStream(bi);

            obj = oi.readObject();
            bi.close();
            oi.close();
        } catch (Exception e) {
            logger.error("字节转对象出错：" + e.getMessage());
            e.printStackTrace();
        }
        return obj;
    }
}
